package com.house.Reborn.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.house.Reborn.dao.PersonRepository;
import com.house.Reborn.model.Person;
import com.house.Reborn.model.result.Result;
import com.house.Reborn.model.result.ResultSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by admin on 2017/6/22.
 */
@RestController
@RequestMapping("/rb/data")
@Api(value = "测试类",tags = "测试接口")
public class DataController {


    private static final Logger LOGGER = LoggerFactory.getLogger(DataController.class);

    @Autowired
    PersonRepository personRepository;
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "name", value = "姓名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "address", value = "地址", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "age", value = "年龄", required = true, paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value="插入客户信息", notes="注册客户信息", produces = "application/json")
    @ApiResponses(value = {
            @ApiResponse(code=200, message="请求ok"),
            @ApiResponse(code=201, message="请求不ok", response = Exception.class)
    })
    public Result save(String name, String address, Integer age) {
        Result<Person> result = new ResultSupport<Person>();
        Person person = null;
        try{
            person = personRepository.save(new Person(null, name, age, address));
        }catch (Exception e){

        }

        if (person != null) {
            result.setCode("200");
            result.setMsg("保存成功");
            result.setModel(person);
        } else {
            result.setCode("201");
            result.setMsg("保存失败");
            //result.setModel(person);
        }
        LOGGER.info("result:{}",result);
        return result;
    }

    @RequestMapping(value = "/q1", method = RequestMethod.POST)
    @ApiOperation("根据地址查询人物信息")
    public List<Person> q1(String address) {
        List<Person> people = personRepository.findByAddress(address);
        LOGGER.info("result是people：{}",people);
        return people;
    }

    @RequestMapping(value = "/q2", method = RequestMethod.POST)
    @ApiOperation("根据地址、姓名查询人物信息")
    public Person q2(String name, String address) {
        Person people = personRepository.findByNameAndAddress(name, address);
        return people;
    }

    @RequestMapping(value = "/q3", method = RequestMethod.POST)
    @ApiOperation("根据地址、姓名自定义查询人物信息")
    public Person q3(String name, String address) {
        Person person = personRepository.withNameAndAddressQuery(name, address);
        return person;
    }

    @RequestMapping(value = "/q4", method = RequestMethod.POST)
    @ApiOperation("根据地址、姓名自定义查询人物信息2")
    public Person q4(String name, String address) {
        Person person = personRepository.withNameAndAddressNamedQuery(name, address);
        return person;
    }
    @RequestMapping(value = "/sort", method = RequestMethod.POST)
    @ApiOperation("查询所有人物信息（年龄正序）")
    public List<Person> sort() {
        List<Person> people = personRepository.findAll(new Sort(Sort.Direction.ASC, "age"));
        return people;
    }
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiOperation("查询分页人物信息")
    public Page<Person> page(int page, int size){
        Page<Person> all = personRepository.findAll(new PageRequest(page, size));
        return all;
    }
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    @ApiOperation("查询所有人物信息")
    public List<Person> all(){
        return personRepository.findAll();
    }
}
